package utils

import (
	"time"
)

// Fn 定义函数类型
type Fn func() error

// ITicker 定时器中的成员
type ITicker struct {
	ITick  *time.Ticker
	Runner Fn
}

func NewITick(interval time.Duration, f Fn) *ITicker {
	return &ITicker{
		ITick:  time.NewTicker(interval),
		Runner: f,
	}
}

// Start 启动定时器需要执行的任务
func (t *ITicker) Start() {
	for {
		select {
		case <-t.ITick.C:
			t.Runner()
		}
	}
}

// Stop 停止定时器
func (t *ITicker) Stop() {
	t.ITick.Stop()
}
